//
//  UserRealNameFillController.m
//  CheekiOS
//
//  Created by mac on 2017/6/20.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserRealNameFillController.h"
#import "HSLimitText.h"
#import "BXTextField.h"
#import "UserEditIdentityCardController.h"
#import "UserViewController.h"
#define NUMBERSX @"0123456789X"
@interface UserRealNameFillController ()<HSLimitTextDelegate,UITextFieldDelegate>

@property (nonatomic, strong)HSLimitText *nametextView;
@property (nonatomic, strong)BXTextField *IdCardView;
@property (nonatomic, strong)NSString *cardStr;
@property (nonatomic, strong)UIButton *btn;

@end

@implementation UserRealNameFillController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavgationBarHiddenNo];
    [self selfTitleFontColor];

    self.title = @"实名认证";
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, KScreenWidth, 121)];
    topView.backgroundColor = [UIColor whiteColor];
    
    UILabel *bglinLab = [UILabel new];
    bglinLab.backgroundColor = RGB(246, 246, 246);
    bglinLab.frame =CGRectMake(0, 0, KScreenWidth, 10);
    [topView addSubview:bglinLab];
    
    UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectMake(15, bglinLab.bottom, 60,  topView.height/2-5)];
    nameLab.text = @"真实姓名";
    nameLab.font = FontP(15);
    nameLab.textColor = [UIColor blackColor];
    [topView addSubview:nameLab];
    
    
    _nametextView = [[HSLimitText alloc] initWithFrame:CGRectMake(nameLab.right, bglinLab.bottom,KScreenWidth-15-nameLab.width, topView.height/2-5) type:TextInputTypeTextfield];
    _nametextView.type =TextInputTypeTextfield;
    _nametextView.delegate = self;
    _nametextView.placeholder = @"请输入您的真实姓名";
    _nametextView.isBecomFirstResponder = NO;
    _nametextView.tag = 1;
    
    [topView addSubview:_nametextView];
    
    
    
    
    UILabel *linlab = [UILabel new];
    linlab.frame = CGRectMake(0, _nametextView.bottom, KScreenWidth, 1);
    linlab.backgroundColor = TheLinColor;
    [topView addSubview:linlab];
    
    UILabel *CardLab = [[UILabel alloc] initWithFrame:CGRectMake(15, linlab.bottom, 60, topView.height/2)];
    CardLab.text = @"身份证号";
    CardLab.font = FontP(15);
    CardLab.textColor = [UIColor blackColor];
    [topView addSubview:CardLab];
    
    
    _IdCardView = [[BXTextField alloc] initWithFrame:CGRectMake(CardLab.right+10, linlab.bottom, KScreenWidth-CardLab.width-15, topView.height/2)];
    _IdCardView.delegate = self;
    _IdCardView.font = FontP(15);
    _IdCardView.placeholder = @"请输入您的身份证号";
    _IdCardView.tag = 2;
    
    
    [topView addSubview:_IdCardView];
    
    [self.view addSubview:topView];
    
    UILabel *labT = [UILabel new];
    labT.frame = CGRectMake(15, topView.bottom, KScreenWidth-15, 30);
    labT.font = FontP(12);
    labT.textColor = RGB(177, 178, 180);
    labT.textAlignment = NSTextAlignmentLeft;
    labT.text = @"请用本人信息认证，不可冒用他人身份证信息";
    [self.view addSubview:labT];
    
    
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btn addTarget:self action:@selector(didClickReal:) forControlEvents:UIControlEventTouchUpInside];
    [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_btn setTitle:@"提交" forState:UIControlStateNormal];
    _btn.backgroundColor = Colorbackground;
    _btn.frame = CGRectMake(24, topView.bottom+100, KScreenWidth-48, 50);
    _btn.tag = 1;
    _btn.layer.cornerRadius=25;
    _btn.titleLabel.font = FontP(15);
    
    [self.view addSubview:_btn];
    
}

- (void)didClickReal:(UIButton *)sender{
    if (sender.tag == 1) {
        if (self.cardStr.length == 0 || self.nametextView.text.length == 0) {
            [SVProgressHUD showInfoWithStatus:@"请正确填写真实姓名和身份证信息"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                
            });
            return;
        }
        [HcardHttpmanager getUserCardValidateName:self.nametextView.text idcardnum:_cardStr success:^(id sucess) {
            
            NSLog(@"%@",sucess);
            if([sucess[@"code"] isEqualToString:@"ok"]){
                
                [SVProgressHUD showInfoWithStatus:@"信息提交成功,正在审核请等待"];
                for (UIViewController *temp in self.navigationController.viewControllers) {
                    if ([temp isKindOfClass:[UserViewController class]]) {
                        
                        [self.navigationController popToViewController:temp animated:YES];
                        return;
                    }
                }

            } else{
                
                [SVProgressHUD showErrorWithStatus:sucess[@"message"]];
                [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [_btn setTitle:@"该身份已认证,如非您本人请点击重新认证" forState:UIControlStateNormal];
                _btn.backgroundColor = [UIColor blackColor];
                _btn.tag = 2;
                
            }
            
            
        } failure:^(NSError *error) {
            
        }];
        

    }else{
        UserEditIdentityCardController *cardEdit = [[UserEditIdentityCardController alloc] init];
        [self.navigationController pushViewController:cardEdit animated:YES];
    
    }
    
    
}


#pragma mark============= textFiled Delegate     ===========
- (void)limitTextLimitInput:(HSLimitText *)textLimitInput text:(NSString *)text
{
    if([textLimitInput isEqual:self.nametextView]){
        self.nametextView.text = text;
    }
    
}

- (void)limitTextLimitInputOverStop:(HSLimitText *)textLimitInput
{
    NSLog(@"%@",@"超出什么都不做 直接截断");
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField.tag ==2 ){
        NSCharacterSet*cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERSX]invertedSet];
        NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest) {
            
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"请正确输入身份证号"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            [alert show];
            return NO;
        }else{
            NSString *text = [textField text];
            NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789X"];
            string = [string stringByReplacingOccurrencesOfString:@"" withString:@""];
            if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
                return NO;
            }
            text = [text stringByReplacingCharactersInRange:range withString:string];
            text = [text stringByReplacingOccurrencesOfString:@"" withString:@""];
            
            NSString *newString = @"";
            while (text.length > 0) {
                NSString *subString = [text substringToIndex:MIN(text.length, 4)];
                newString = [newString stringByAppendingString:subString];
                if (subString.length == 4) {
                    newString = [newString stringByAppendingString:@""];
                }
                text = [text substringFromIndex:MIN(text.length, 4)];
            }
            newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
            // 限制长度
            if (newString.length >= 19) {
                UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请正确输入身份证号" message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                [alertVC addAction:cancelAction];
                
                [self.navigationController presentViewController:alertVC animated:YES completion:nil];
                
                return NO;
            }
            [textField setText:newString];
            _cardStr =textField.text;//证件
            return NO;
            
        }
    }
    return YES;
}



@end
